package com.yun.baymax.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

import com.yun.baymax.R;
import com.yun.lib.entry.ThemeChanged;
import com.yun.lib.utils.SPUtils;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

public abstract class BaseActivity extends AppCompatActivity {

    private static final String TITLE = "title";
    protected Context mContext;
    protected long backTime;
    private EventBus eventBus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        init();
        if (setLayoutResourceId() != 0)
            setContentView(setLayoutResourceId());
        mContext = this;
        eventBus = EventBus.getDefault();
        eventBus.register(this);
        initVariables();
        initViews(savedInstanceState);
        loadData();
    }

    protected void init() {
        String themeName = (String) SPUtils.get(this, "theme", "");
        switch (themeName) {
            case "少女粉":
                setTheme(R.style.PinkTheme);
                break;
            case "姨妈红":
                setTheme(R.style.RedTheme);
                break;
            case "闷骚紫":
                setTheme(R.style.PurpleTheme);
                break;
            case "咸蛋黄":
                setTheme(R.style.YellowTheme);
                break;
            case "草帽绿":
                setTheme(R.style.GreenThemeLight);
                break;
            case "原谅绿":
                setTheme(R.style.GreenThemeDark);
                break;
            default:
                setTheme(R.style.AppTheme_NoActionBar);
                break;
        }
    }

    /**
     * @return 布局文件
     */
    protected abstract int setLayoutResourceId();

    /**
     * 初始化变量，其中包括intent传递的参数
     */
    protected abstract void initVariables();

    /**
     * @param savedInstanceState 初始化控件，并设置事件方法
     */
    protected abstract void initViews(Bundle savedInstanceState);

    /**
     * 加载数据
     */
    protected abstract void loadData();

    /**
     * 不传递参数
     *
     * @param clazz 需要启动的Activity
     */
    protected void startActivity(Class<?> clazz) {
        startActivity(new Intent(this, clazz));
    }


    /**
     * 传递参数
     *
     * @param clazz
     * @param bundle
     */
    protected void startActivityWithExtras(Class<?> clazz, Bundle bundle) {
        Intent intent = new Intent(this, clazz);
        intent.putExtra("extras", bundle);
        startActivity(intent);
    }

    protected <T extends View> T $(@IdRes int id) {
        return (T) findViewById(id);
    }

    public void showToast(String msg) {

        Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();

    }

    public void goNext(Bundle name, Class c) {
        Intent i = new Intent(this, c);
        i.putExtra("bundle", name);
        startActivity(i);
    }

    /**
     * 提示是否退出应用 连续两次返回后退出
     */
    protected boolean toastBack() {
        return true;
        /*long now = System.currentTimeMillis();
        if (now - backTime < 2000) {
            finish();
            return true;
        } else {
            Toast.makeText(this, R.string.back_remind, Toast.LENGTH_SHORT).show();
            backTime = now;
            return false;
        }*/
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        eventBus.unregister(this);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(ThemeChanged themeChanged) {
        recreate();
    }
}
